package com.ysqcould.web.controller.content;


import com.alibaba.fastjson.JSONObject;
import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.content.domain.Advertising;
import com.ysqcould.content.domain.Notice;
import com.ysqcould.content.service.IAdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

import static com.ysqcould.allocation.utils.MultipartFileUtils.getFilePath;

/**
 * 广告Controller
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/content/advertising")
public class AdvertisingController extends BaseController
{
    @Autowired
    private IAdvertisingService advertisingService;

    /**
     * 查询广告列表
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:list')")
    @GetMapping("/list")
    public TableDataInfo list(Advertising advertising)
    {
        startPage();
        List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
        return getDataTable(list);
    }

/*    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("123456");
        boolean matches = new BCryptPasswordEncoder().matches("123456", "$2a$10$P9.K8kH5XlV3HYtJteIpH.3i68eKRTqU.HSr.jinNbic1t/B7lM0e");

        System.out.println(encode);
    }*/

    /**
     * 导出广告列表
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:export')")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Advertising advertising)
    {
        List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
        ExcelUtil<Advertising> util = new ExcelUtil<Advertising>(Advertising.class);
        util.exportExcel(response, list, "广告数据");
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "广告封面上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        final JSONObject path = getFilePath(file);
        final String pictures = UploadFileUtil.AliOSSUpload((InputStream) path.get("in"), (String)path.get("name"));
        return AjaxResult.success("上传成功",pictures);
    }

    /**
     * 获取广告详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(advertisingService.selectAdvertisingById(id));
    }

    /**
     * 新增广告
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:add')")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Advertising advertising)
    {
        advertising.setAsId(getUserId());
        return toAjax(advertisingService.insertAdvertising(advertising));
    }

    /**
     * 修改广告
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:edit')")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Advertising advertising)
    {
        return toAjax(advertisingService.updateAdvertising(advertising));
    }

    /**
     * 删除广告
     */
    @PreAuthorize("@ss.hasPermi('content:advertising:remove')")
    @Log(title = "广告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(advertisingService.deleteAdvertisingByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Advertising advertising)
    {
        return toAjax(advertisingService.updateStatus(advertising));
    }
}
